% calls IBCmodel
% version november 2010
% runs several model variants

clc; clear all; close all;

global namevec nk npts f p positionshock gy


saveirf      = 1;

npts = 30;
time = 0:npts-1;

%% model parameters
marktescomplete = 1;
adjcostCEE      = 1;
chi             = 2.48;
gy              = .19;        
importshare     = 0.12;

%% fiscal policy
rhog            = .9;
phitg           = 0;
psiy            = 0;

%% monetary policy
fixrate         = 0;


%% Preferences
hab     = 0;
gamma           = 1;
sigma           = .66;

%% simulate model

% 1: closed economy (n=1), cgg = 0, noinv
% 2: as 1, but g does not respond to debt, it is reduced permanently instead
% 3: as 1, but g does not respond to debt, tax finance
% 4: as 1, but no nominal rigidities
% 5: as 1, but n = 0.2
% 6: n = 0.2 and g does not respond to debt (tax finance)
% 7: as 5, but with investment and cgg-rule
% 8: as 6, but with investment and cgg-rule
% 9: as 1, but psi_t = 0.02;
% 10: as in 1, but phi_pi = 5
% 11: as in 5, but cpi targeting 

for jj=1:11;
    disp(['Running specification ' num2str(jj) ])
    rho_comp = .99;
    nolongrate = 0;
    psi_g            = 0.02;
    psi_t            = 0.02;
    xi              = .75;
    xiw             = 0.83;
    cgg00           = 0;   % clarida-gali-gertler 2000 specification for post-82 sample
    noinv           = 1;   % for noinv = 0 you need cgg00 on
    n               = 1;   
    distax          = 0 ; 
    cpitarg         = 0;
    avtarg = 0;       % mon pol stabilizes weighted averages of price and wage inflation

    
  
  
    if jj==2 || jj==3 || jj==6 || jj ==8;
        psi_g            = 0;
    end
    
    if jj==2;
        rho_comp = 1;
        nolongrate = 1;
    end
    
    if jj==4;
        xi              = 0;
        xiw             = 0;
    end
        
    if jj==5 || jj ==6 || jj==7 || jj==8 || jj==11 ; 
        n = 0.2;
    end
    
    if jj == 7 || jj == 8;
        cgg00           = 1;  
        noinv           = 0;  
    end
    
        
    
    if ~cgg00 
        rhoi            = 0;
        phipi           = 1.5;
        if jj==10;
            phipi = 5; avtarg = 0;
        end
        
        phigap          = 0;
    elseif cgg00
        rhoi            = 0.91;
        phipi           = 1.58;
        phigap          =.14/4;
    end

    
    if jj==11;
        cpitarg =1; 
    end
    % call model
    params=[chi; xi; xiw; rhog; sigma; gamma; 
        marktescomplete; adjcostCEE;fixrate; psi_t;
        psi_g; rhoi; phipi; phitg; psiy; gy; n; hab; phigap; rho_comp;
        cgg00; noinv; nolongrate; importshare; distax; cpitarg; avtarg ];
    
    [f,p,nk,namevec,css,nss,cy,xss,yss,A,B]= IBCmodel(params);
    
    % IRF
    vector=zeros(nk,1);
    
    vector(loc(namevec, 'ga1'))=1/gy;
    if jj == 2;
        positionshock = loc(namevec,'e0');
        options      = optimset('Display','iter','TolFun',1e-9,'TolX',1e-9,'MaxFunEvals',500000,'MaxIter',100000);
        [solution,fval,residual,exitflag,output,lmult,jacobi] = lsqnonlin('fitit',0,-100,100,options); 
        disp(solution)
        vector(positionshock)=solution;
    end   
%         
%       
    
    R1=zeros(npts,nk);
    R2=zeros(npts,length(namevec)-nk);
    R1(1,:)=(vector)';
    R2(1,:)=(f*vector)';
    for i=2:npts 
        R1(i,:)=(p^(i-1)*vector)';
        R2(i,:)=(f*p^(i-1)*vector)';    
    end
    
    resp(:,:,jj) = [R1 R2];
    dpdv=squeeze(resp(1,loc(namevec,'PDV'),jj));
 
    
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% plot figures: paper
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% figure 1

indexset=[loc(namevec,'Government spending')  loc(namevec,'Long-term real rate') loc(namevec,'Inflation') ...
    loc(namevec,'Consumption') loc(namevec,'Output') loc(namevec,'Policy rate') ];
  
 namshort = char('g','lrr','pi','c','y','pr');     
     
 figure
 jm=1; 
for kk=indexset;
    figure
        plot(time,squeeze(resp(:,kk,1)),'b','LineWidth',2,'MarkerSize',1);       
        hold on
        plot(time,squeeze(resp(:,kk,2)),'k-.','LineWidth',2,'MarkerSize',1);        
        
        plot(time,squeeze(resp(:,kk,3)),'r--','LineWidth',2,'MarkerSize',1);        
        
        plot(time,zeros(length(time),1),'k--','LineWidth',1)
%            title(deblank(namevec(kk,:)),'FontSize',14);    
              minval = min([squeeze(resp(:,kk,1)); squeeze(resp(:,kk,2)); squeeze(resp(:,kk,3))]);
        maxval = max([squeeze(resp(:,kk,1)); squeeze(resp(:,kk,2)); squeeze(resp(:,kk,3))]);
        % axis tight
        axis([0 time(end)  1.5*min([minval 0]) 1.5*max(maxval)]);box on;     
        set(gca,'FontSize',25)

        saveas(gcf,['..\figures\fig1_' deblank(namshort(jm,:)) '.eps'],'psc2')
        jm=jm+1;
  
end



indexset=[loc(namevec,'Government spending')  loc(namevec,'Real interest rate') loc(namevec,'Inflation') ...
    loc(namevec,'Consumption') loc(namevec,'Long-term real rate') loc(namevec,'Policy rate') loc(namevec,'Taxes') ];
  
     
 figure
 jm=1; 
for kk=indexset;
    
    subplot(3,3,jm)
        plot(time,squeeze(resp(:,kk,1)),'b','LineWidth',2,'MarkerSize',1);       
        hold on
       % plot(time,squeeze(resp(:,kk,2)),'k-.','LineWidth',2,'MarkerSize',1);        
        
        plot(time,squeeze(resp(:,kk,3)),'r--','LineWidth',2,'MarkerSize',1);        
        
        plot(time,zeros(length(time),1),'k--','LineWidth',1)
            title(deblank(namevec(kk,:)),'FontSize',14);    
        axis tight
        %set(gca,'FontSize',25)

        jm=jm+1;
  
end

%% figure 2

indexset=[loc(namevec,'Government spending')  loc(namevec,'Output') loc(namevec,'Inflation') ...
    loc(namevec,'Consumption') loc(namevec,'Long-term real rate') loc(namevec,'Policy rate') loc(namevec,'Real interest rate')];
  
 namshort = char('g','y','pi','c','lrr','pr','rr');       
 figure
 jm=1; 
for kk=indexset;
    figure
    %subplot(2,3,jm)
        plot(time,squeeze(resp(:,kk,1)),'b','LineWidth',2,'MarkerSize',1);       
        hold on
        plot(time,squeeze(resp(:,kk,4)),'r--','LineWidth',2,'MarkerSize',1);        
        
      %  plot(time,squeeze(resp(:,kk,2)),'k-.','LineWidth',2,'MarkerSize',1);        
          
        plot(time,zeros(length(time),1),'k--','LineWidth',1)
 %           title(deblank(namevec(kk,:)),'FontSize',16);    
        minval = min([squeeze(resp(:,kk,1)); squeeze(resp(:,kk,4))]);
        maxval = max([squeeze(resp(:,kk,1)); squeeze(resp(:,kk,4))]);
        % axis tight
        axis([0 time(end)  1.5*min([minval 0]) 1.5*max(maxval)]);box on;     
        set(gca,'FontSize',25)
       if jm == 1;
        %   legend('Nominal rigidities','Without')
       end
        saveas(gcf,['..\figures\fig2_' deblank(namshort(jm,:)) '.eps'],'psc2')
        jm=jm+1;
  
end

%% plot figure 3
indexset=[loc(namevec,'Government spending') loc(namevec,'Output') ...
     loc(namevec,'Inflation') loc(namevec,'Consumption') loc(namevec,'Policy rate') ...
    loc(namevec,'Real interest rate')    loc(namevec,'Long-term real rate') ...
     loc(namevec,'Real exchange rate') ... 
    loc(namevec,'Trade balance') ];

namshort = char('g','y','pi','c','pr','rr','lr','rx','tb');       
jkm=1;jm=1; 
for kk=indexset;
    figure
    %subplot(3,3,jm)
    plot(time,squeeze(resp(:,kk,1)),'b','LineWidth',2,'MarkerSize',1);       
    hold on
    plot(time,squeeze(resp(:,kk,5)),'r--','LineWidth',2,'MarkerSize',1);        
    %plot(time,squeeze(resp(:,kk,6)),'k-.','LineWidth',2,'MarkerSize',1);        
    plot(time,zeros(length(time),1),'k:','LineWidth',1)
    minval = min([squeeze(resp(:,kk,9)) ; squeeze(resp(:,kk,6)); squeeze(resp(:,kk,5))]);
    maxval = max([squeeze(resp(:,kk,9)); squeeze(resp(:,kk,6)) ; squeeze(resp(:,kk,5))]);
    axis([0 time(end)  1.5*min([minval 0]) 1.5*max(maxval)]);box on;     
    set(gca,'FontSize',25)


    saveas(gcf,['..\figures\fig3_' deblank(namshort(jm,:)) '.eps'],'psc2')
    jm=jm+1;
end





%% plot figure 4

indexset=[loc(namevec,'Government spending') loc(namevec,'Output') ...
     loc(namevec,'Inflation') loc(namevec,'Consumption') loc(namevec,'Investment') loc(namevec,'Policy rate') ...
      loc(namevec,'Long-term real rate') loc(namevec,'Real exchange rate')  ... 
    loc(namevec,'Trade balance') loc(namevec,'Real interest rate')];

    namshort = char('g','y','pi','c','i','pr','lr','rx','tb','rr');       
    %figure
    jkm=1;jm=1; 
for kk=indexset;
    figure
    plot(time,squeeze(resp(:,kk,7)),'b','LineWidth',2,'MarkerSize',1);       
    hold on
    plot(time,squeeze(resp(:,kk,8)),'r--','LineWidth',2,'MarkerSize',1);        

    plot(time,zeros(length(time),1),'k:','LineWidth',1)
    set(gca,'FontSize',25)
    if jm==1;
        axis([0 time(end) -.7 1.5]);
    elseif jm==2;
        axis([0 time(end) -.7 1.5]);
    elseif jm==4;
        axis([0 time(end) -.3 .3]);
     elseif jm==8;
        axis([0 time(end) -.3 .7]);
    end
    box on;     
    saveas(gcf,['..\figures\fig4_' deblank(namshort(jm,:)) '.eps'],'psc2')

    jm=jm+1;
end


indexset=[loc(namevec,'Output^*') ...
     loc(namevec,'pib2') loc(namevec,'Consumption2') loc(namevec,'i2') ...
    loc(namevec,'rr2')    ];

namshort = char('yF','piF','cF','prF','rrF');       
jkm=1;jm=1; 
for kk=indexset;
    figure
    %subplot(3,3,jm)
 plot(time,squeeze(resp(:,kk,7)),'b','LineWidth',2,'MarkerSize',1);       
    hold on
    plot(time,squeeze(resp(:,kk,8)),'r--','LineWidth',2,'MarkerSize',1);        
   %plot(time,squeeze(resp(:,kk,6)),'k-.','LineWidth',2,'MarkerSize',1);        
    plot(time,zeros(length(time),1),'k:','LineWidth',1)
    minval = min([squeeze(resp(:,kk,8)) ; squeeze(resp(:,kk,7)); ]);
    maxval = max([squeeze(resp(:,kk,8)); squeeze(resp(:,kk,7)) ; ]);
    axis([0 time(end)  1.5*min([minval 0]) 1.5*max(maxval)]);box on;     
    set(gca,'FontSize',25)


    saveas(gcf,['..\figures\fig4_' deblank(namshort(jm,:)) '.eps'],'psc2')
    jm=jm+1;
end
close all


